package com.eh.taxation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eh.commonutils.R;
import com.eh.taxation.entity.Question;
import com.eh.taxation.entity.vo.QuestionQuery;
import com.eh.taxation.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 问题 前端控制器
 * </p>
 *
 * @author erhuo
 * @since 2021-01-21
 */
@RestController
@RequestMapping("/taxation/question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    // 分页条件查询问答
    @PostMapping("getQuestionListPage/{current}/{limit}")
    public R getQuestionListPage(@PathVariable long current,
                                 @PathVariable long limit,
                                 @RequestBody(required = false) QuestionQuery questionQuery) {
        Page<Question> questionPage = new Page<>(current, limit);
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(questionQuery.getSubject_id())) wrapper.eq("subject_id", questionQuery.getSubject_id());
        if (!StringUtils.isEmpty(questionQuery.getSubject_parent_id())) wrapper.eq("subject_parent_id", questionQuery.getSubject_parent_id());
        if (!StringUtils.isEmpty(questionQuery.getBegin())) wrapper.ge("gmt_create", questionQuery.getBegin());
        if (!StringUtils.isEmpty(questionQuery.getEnd())) wrapper.le("gmt_create", questionQuery.getEnd());
        wrapper.orderByDesc("gmt_create");
        questionService.page(questionPage, wrapper);
        return R.ok().data("total", questionPage.getTotal())
                     .data("items", questionPage.getRecords());
    }

    // 根据id删除问答
    @DeleteMapping("deleteQuestionById/{id}")
    public R deleteQuestionById(@PathVariable String id) {
        questionService.removeById(id);
        return R.ok();
    }

    // 添加问题
    @PostMapping("addAnswerInfo")
    public R addAnswerInfo(@RequestBody Question question) {
        questionService.save(question);
        return R.ok();
    }

    // 修改问题
    @PostMapping("updateAnswerInfo")
    public R updateAnswerInfo(@RequestBody Question question) {
        questionService.updateById(question);
        return R.ok();
    }

    // 根据id查询问答
    @GetMapping("getQuestionInfoId/{id}")
    public R getQuestionInfoId(@PathVariable String id) {
        Question question = questionService.getById(id);
        return R.ok().data("question", question);
    }

}

